﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double a = 0, b = 0, e_2 = 0, e_dot_2 = 0,A1,S, A21, rou = 206264.806247096355;
            int pp = 0;
            if (radioButton1.Checked)
            {
                a = 6378245;//椭圆长半轴
                b = 6356863.0187730473;//椭圆短半轴
                e_2 = 0.006693421622966;//椭圆第一偏心率e^2
                e_dot_2 = 0.006738525414683;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (radioButton2.Checked)//
            {
                a = 6378140;//椭圆长半轴
                b = 6356755.2881575287;//椭圆短半轴
                e_2 = 0.006694384999588;//椭圆第一偏心率e^2
                e_dot_2 = 0.006739501819473;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (radioButton3.Checked)//
            {
                a = 6378137;//椭圆长半轴
                b = 6356752.3142;//椭圆短半轴
                e_2 = 0.00669437999013;//椭圆第一偏心率e^2
                e_dot_2 = 0.00673949674227;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (radioButton4.Checked)//
            {
                a = 6378137;//椭圆长半轴
                b = 6356752.3141;//椭圆短半轴
                e_2 = 0.00669438002290;//椭圆第一偏心率e^2
                e_dot_2 = 0.00673949677548;//椭圆第二偏心率e'^2 
                pp = 1;
            }
            else if (pp == 0)
            {
                MessageBox.Show("请选择椭球体", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            double c = a * a / b;
            //转化为弧度制
            double B1 = (Convert.ToDouble(textBox1.Text) + Convert.ToDouble(textBox2.Text) / 60 + Convert.ToDouble(textBox3.Text) / 3600) / 180 * Math.PI;
            double L1 = (Convert.ToDouble(textBox4.Text) + Convert.ToDouble(textBox5.Text) / 60 + Convert.ToDouble(textBox6.Text) / 3600) / 180 * Math.PI;
            double B2 = (Convert.ToDouble(textBox7.Text) + Convert.ToDouble(textBox8.Text) / 60 + Convert.ToDouble(textBox9.Text) / 3600) / 180 * Math.PI;
            double L2 = (Convert.ToDouble(textBox10.Text) + Convert.ToDouble(textBox11.Text) / 60 + Convert.ToDouble(textBox12.Text) / 3600) / 180 * Math.PI;
            

            double Bm = (B1 + B2) / 2,Lm = (L1 + L2) / 2;
            double tm = Math.Tan(Bm), nm = e_dot_2 * Math.Cos(Bm) * Math.Cos(Bm);
            double Wm = Math.Sqrt(1 - e_2 * Math.Sin(Bm) * Math.Sin(Bm));
            double Vm = Math.Sqrt(1 + e_dot_2 * Math.Cos(Bm) * Math.Cos(Bm));
            double Mm = a * (1 - e_2) / (Wm * Wm * Wm);
            double Nm = a / Wm;
            double r01 = Nm / rou * Math.Cos(Bm);
            double r21 = Nm * Math.Cos(Bm) / 24 / Math.Pow(Vm, 4) / Math.Pow(rou, 3) * (1 + nm * nm - 9 * Math.Pow(nm * tm * nm * nm, 2));
            double r03 = -Nm / 24 / Math.Pow(rou, 3) * Math.Pow(Math.Cos(Bm), 3) * tm * tm;
            double S10 = Nm / rou / Vm / Vm;
            double S12 = Nm * Math.Cos(Bm) * Math.Cos(Bm) / 24 / Math.Pow(Vm, 2) / Math.Pow(rou, 3) * (2 + 3 * tm * tm + 2 * nm * nm);
            double S30 = Nm / 8 / Math.Pow(rou, 3) / Math.Pow(Vm, 6) * (nm * nm - tm * tm * nm * nm);
            double delta_B = (B2 - B1) * rou, delta_L = (L2 - L1) * rou;
            double SsinAm = r01 * delta_L + r21 * delta_B * delta_B * delta_L + r03 * Math.Pow(delta_L, 3);
            double ScosAm = S10 * delta_B + S12 * delta_B * delta_L * delta_L + S30 * Math.Pow(delta_B, 3);

            double t01 = tm * Math.Cos(Bm);
            double t21 = Math.Cos(Bm) * tm * (2 + 7 * nm * nm + 9 * tm * tm * nm * nm + 5 * Math.Pow(nm, 4)) / 24 / rou / rou / Math.Pow(Vm, 4);
            double t03 = Math.Pow(Math.Cos(Bm), 3) * tm * (2 + tm * tm + 2 * nm * nm) / 24 / rou / rou;
            double delta_A = t01 * delta_L + t21 * delta_B * delta_B * delta_L + t03 * Math.Pow(delta_L, 3);//单位秒
            double Am = Math.Atan(SsinAm / ScosAm);
            if (Am < 0) Am = Am + 2*Math.PI;
            
            A1 = Am - delta_A / 2 /rou;
            A21 = Am + delta_A / 2/rou ;
            if (A1 > Math.PI) A21 = A21 - Math.PI;
            if (A1 < Math.PI) A21 = A21 + Math.PI;
            S = ScosAm / Math.Cos(Am);
            textBox13.Text = Convert.ToString(S);
            //打印A12
            A1 = A1 * 180 / Math.PI;
            textBox19.Text = Convert.ToString(Math.Floor(A1));
            textBox18.Text = Convert.ToString(Math.Floor((A1 - Math.Floor(A1)) * 60));
            textBox17.Text = Convert.ToString(((A1 - Math.Floor(A1)) * 60 - Math.Floor((A1 - Math.Floor(A1)) * 60)) * 60);
            //打印A21
            A21 = A21 * 180 / Math.PI;
            textBox16.Text = Convert.ToString(Math.Floor(A21));
            textBox15.Text = Convert.ToString(Math.Floor((A21 - Math.Floor(A21)) * 60));
            textBox14.Text = Convert.ToString(((A21- Math.Floor(A21)) * 60 - Math.Floor((A21 - Math.Floor(A21)) * 60)) * 60);


        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox4.Text = string.Empty;
            textBox5.Text = string.Empty;
            textBox6.Text = string.Empty;
            textBox7.Text = string.Empty;
            textBox8.Text = string.Empty;
            textBox9.Text = string.Empty;
            textBox10.Text = string.Empty;
            textBox11.Text = string.Empty;
            textBox12.Text = string.Empty;
            textBox13.Text = string.Empty;
            textBox14.Text = string.Empty;
            textBox15.Text = string.Empty;
            textBox16.Text = string.Empty;
            textBox17.Text = string.Empty;
            textBox18.Text = string.Empty;
            textBox19.Text = string.Empty;
        }
    }
}
